package com.sinog.front.controller.basic;

import com.sinog.core.domain.Department;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.basic.DepartmentService;
import com.sinog.front.feign.basic.UserService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @desc 系统管理 - 部门管理 - 部门管理 - DeaprtmentController
 * @author hak
 * @date 2019年7月1日 12点54分
 */
@RestController
public class DeaprtmentController {

    @Autowired
    private UserService userService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private UserFacade userFacade;

    /**
     * 系统管理 - 部门管理 - 部门管理 - 保存部门信息
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存部门信息")
    @PostMapping("/basic/department")
    public Result save(HttpServletRequest request,String data) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Result result = userService.selectOne(authentication.getName(),"0");
        UserDTO userDTO;
        if(Constants.Number.NUM_ONE.equals(result.getCode())) {
            String data1 = (String) result.getData();
            userDTO = JsonUtil.toBean(data1, UserDTO.class);
        } else {
            userDTO = new UserDTO();
        }
        if(Constants.RoleId.ROLE_ID_AB.equals(userDTO.getRoleid()) || Constants.RoleId.ROLE_ID_SFT.equals(userDTO.getRoleid())) {
            return departmentService.save(data);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 部门管理 - 部门管理 - 懒加载部门树
     * @param id 选中节点id
     * @return String
     */
    @RequestMapping("/basic/departments/loadTree")
    public String loadTree(@RequestParam(required = false) String id) {
        //当前登录账号
        UserDTO authentication = userFacade.getAuthentication();
        if(null == authentication){
            authentication = new UserDTO();
        }
        List<String> childList;
        //id有值
        if(id != null && id.contains(Constants.StrFinalValue.X)) {
            childList = userService.selectAllDepartIdByDepartid(id);
            childList.add(id);
        } else {
            //id没值，选择当前账号id
            childList = userService.selectAllDepartIdByDepartid(authentication.getDepartid());
        }
        if(childList.contains(id) || null == id) {
            Result result;
            String pid = authentication.getOrgid();
            if(Objects.equals(id,"") || null == id) {
                id = authentication.getDepartid();
                result = departmentService.getDepartInfo(id,pid,Constants.NumberStr.NUM_STR_ONE);
            } else {
                result = departmentService.getDepartInfo(id,pid,Constants.NumberStr.NUM_STR_TWO);
            }
            String json = "";
            if(Constants.Number.NUM_ONE.equals(result.getCode())) {
                json = JsonUtil.toText(result.getData());
            }
            return json;
        }
        return null;
    }

    /**
     * 系统管理 - 部门管理 - 部门管理 - 获取点击节点的部门信息
     * @param id id
     * @return String
     */
    @RequestMapping("/basic/departments/getDepartInfo")
    public String getDepartInfo(@RequestParam(required = false) String id) {
        //当前登录账号
        UserDTO authentication = userFacade.getAuthentication();
        if(null == authentication){
            authentication = new UserDTO();
        }
        List<String> childList = userService.selectAllDepartIdByDepartid(authentication.getDepartid());
        if(childList.contains(id)) {
            String pid = authentication.getOrgid();
            Result result = departmentService.getDepartInfo(id,pid,Constants.NumberStr.NUM_STR_THREE);
            String json = "";
            if(Constants.Number.NUM_ONE.equals(result.getCode())) {
                json = JsonUtil.toText(result.getData());
            }
            return json;
        }
        return null;
    }

    /**
     * 系统管理 - 部门管理 - 部门管理 - 批量删除部门
     * 如果是父节点,其子节点也删除
     * @param ids ids
     * @param id id
     * @return Result
     */
    @DeleteMapping("/basic/department/delete")
    public Result delete(@RequestParam("ids[]") String[] ids,@RequestParam("id") String id) {
        return departmentService.deleteBatch(ids,id);
    }

    /**
     * 系统管理 - 部门管理 - 部门管理 - 获取下级机构类型下拉
     * @param deptId deptId
     * @return Result
     */
    @RequestMapping("/basic/department/getLevelCombox")
    public Result getLevelCombox(@RequestParam("deptId") String deptId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Result result1 = userService.selectOne(authentication.getName(),"0");
        UserDTO userDTO;
        if(Constants.Number.NUM_ONE.equals(result1.getCode())) {
            String data = (String) result1.getData();
            userDTO = JsonUtil.toBean(data, UserDTO.class);
        } else {
            userDTO = new UserDTO();
        }
        List<String> childList = userService.selectAllDepartIdByDepartid(userDTO.getDepartid());
        if(childList.contains(deptId)) {
            String data = departmentService.getLevelCombox(deptId);
            if(null != data && !data.isEmpty()) {
                return Result.success(data);
            } else {
                return Result.failure(ReturnCode.FAILURE);
            }
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 获取本省下所有监狱
     * @param id id
     * @return List<Department>
     */
    @GetMapping("/azbj/inmates/baseinfo/getAllPrison/{id}")
    public List<Department> getAllPrison(@PathVariable("id") String id) {
        return departmentService.getAllPrison(id);
    }

    /**
     * 预安置管理 - 全部人员管理-入监单位
     * @param departId departId
     * @return List<Map<String,String>>
     */
    @RequestMapping("/basic/department/getrjdanwei")
    public List<Map<String,String>> getrjdanwei(@RequestParam("departId") String departId){
        return departmentService.getrjdanwei(departId);
    }
}